package okhttp3Demo.baidu.news.demo;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import persistence.common.ConnectionProperties;
import persistence.mysql.MysqlConnection;

import java.sql.*;
import java.util.List;

/**
 * Created by Administrator on 2019/7/9.
 */
public class FormatDemo {
    public static ConnectionProperties properties;
    static {
        properties = new ConnectionProperties();
        properties.setServer("localhost");
        properties.setPort(3306);
        properties.setDatabase("data");
        properties.setUsername("root");
        properties.setPassword("");
    }



    public static void main(String[] args) {
        List<News> list = get(10000,10010);
        for (News news : list) {
            System.out.println(JSON.toJSONString(news));
        }
    }

    private static String query_sql = "select date,author,title,company,url,content from news where id = %s";
    public static List<News> get(Integer id, Integer maxId){
        News news = null;
        ResultSet rs = null;
        Connection connection =null;
        Statement statement = null;
        try {
            List<News> list = Lists.newArrayList();
            connection = MysqlConnection.getConnection(properties);
            statement = connection.createStatement();
            while (id <= maxId){
                String sql = String.format(query_sql,id);
                rs = statement.executeQuery(sql);
                while(rs.next()){
                    Date date = rs.getDate("date");
                    String author = rs.getString("author");
                    String title = rs.getString("title");
                    String company = rs.getString("company");
                    String url = rs.getString("url");
                    String content = rs.getString("content");
                    if(content.contains("不良信息举报")){
                        news = new News();
                        news.setDate(date);
                        news.setAuthor(author);
                        news.setTitle(title);
                        news.setCompany(company);
                        news.setUrl(url);
                        list.add(news);
                    }
                }
                id++;
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                rs.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
